home *** CD-ROM | disk | FTP | other *** search
/ MacFormat UK 179 / MF_UK_179_1.iso / DiscContents / In the mag / Widgets / PEMDAS 1.0 / PEMDAS / PEMDAS.wdgt / gui.js < prev    next >
Encoding:
JavaScript  |  2006-12-12  |  30.3 KB  |  979 lines

  1. //PEMDAS 1.0
  2. //Code in the widget bundle (c) 2006 Michael Filippone.
  3. //Code in /System/Library/WidgetResources/AppleClasses/ (c) Apple Computer.
  4. //Visit http://www.donkeyentertainment.com to file bug reports or feature requests.
  5. var thisVersionNumber="1.0", currentReleaseNumber, currentReleaseMessage, currentReleaseURL;
  6. document.addEventListener("keypress", keyPressed, true); 
  7. var answerDiv, formulaDiv, formulaCacheDiv, formulaCacheParentDiv, variableCacheDiv; 
  8. var previousKeyEntered='';
  9. var preferencesAnswerFormatting=1; 
  10. var preferencesProgrammerMode=0; 
  11. var preferencesPrecision=0; 
  12. var preferencesRadians=1; 
  13. var preferencesThousandsSeperators=0; 
  14. var preferencesWhatToCopy=1; 
  15. var lastCheckedTime=0;
  16. var preferencesIgnoreUpdates=0;
  17. var preferencesRemindLater=0;
  18. var commaLocale=0;
  19. var fseLength=4; 
  20. var currentAnswerNumber=1; 
  21. var answer={errorMessage:'1', answer:''};
  22. var formulaScrollTimer;
  23. var currentScrollKey=63235;
  24. var formula='';
  25. var finalAnswer;
  26. var calculationCount=0; 
  27. var justRecalculatedCachedFormula=false;
  28. var justCalculatedEquation=false;
  29. var growboxInset, initialClickOffset, initialClick;
  30. var currentcursorposition=0;
  31. var curformula=1; 
  32. var cursorPixelOffset=22;
  33. var resizeToX=480, resizeToY=385;
  34. var fontwidths=new Array();
  35. fontwidths[33]=3; 
  36. fontwidths[37]=11; 
  37. fontwidths[40]=3; 
  38. fontwidths[41]=3; 
  39. fontwidths[42]=4; 
  40. fontwidths[43]=7; 
  41. fontwidths[44]=3; 
  42. fontwidths[45]=3; 
  43. fontwidths[46]=3; 
  44. fontwidths[47]=3; 
  45. fontwidths[48]=6; 
  46. fontwidths[49]=6; 
  47. fontwidths[50]=6; 
  48. fontwidths[51]=6;
  49. fontwidths[52]=6; 
  50. fontwidths[53]=6;
  51. fontwidths[54]=6; 
  52. fontwidths[55]=6;
  53. fontwidths[56]=6; 
  54. fontwidths[57]=6;
  55. fontwidths[61]=7; 
  56. fontwidths[94]=5; 
  57. fontwidths[65]=8;
  58. fontwidths[66]=8;
  59. fontwidths[67]=8;
  60. fontwidths[68]=8;
  61. fontwidths[69]=8;
  62. fontwidths[70]=7;
  63. fontwidths[71]=9;
  64. fontwidths[72]=8;
  65. fontwidths[73]=3;
  66. fontwidths[74]=6;
  67. fontwidths[75]=8;
  68. fontwidths[76]=6;
  69. fontwidths[77]=10;
  70. fontwidths[78]=8;
  71. fontwidths[79]=9;
  72. fontwidths[80]=8;
  73. fontwidths[81]=9;
  74. fontwidths[82]=8;
  75. fontwidths[83]=8;
  76. fontwidths[84]=7;
  77. fontwidths[85]=8;
  78. fontwidths[86]=8;
  79. fontwidths[87]=11;
  80. fontwidths[88]=8;
  81. fontwidths[89]=8;
  82. fontwidths[90]=7;
  83. fontwidths[97]=6;
  84. fontwidths[98]=6;
  85. fontwidths[99]=6;
  86. fontwidths[100]=6;
  87. fontwidths[101]=6;
  88. fontwidths[102]=3;
  89. fontwidths[103]=6;
  90. fontwidths[104]=6;
  91. fontwidths[105]=2;
  92. fontwidths[106]=2;
  93. fontwidths[107]=6;
  94. fontwidths[108]=2;
  95. fontwidths[109]=10;
  96. fontwidths[110]=6;
  97. fontwidths[111]=6;
  98. fontwidths[112]=6;
  99. fontwidths[113]=6;
  100. fontwidths[114]=3;
  101. fontwidths[115]=6;
  102. fontwidths[116]=3;
  103. fontwidths[117]=6;
  104. fontwidths[118]=6;
  105. fontwidths[119]=8;
  106. fontwidths[120]=6;
  107. fontwidths[121]=6;
  108. fontwidths[122]=6;
  109. fontwidths[192]=8;
  110. fontwidths[193]=8;
  111. fontwidths[194]=8;
  112. fontwidths[195]=8;
  113. fontwidths[196]=8;
  114. fontwidths[197]=8;
  115. fontwidths[198]=12;
  116. fontwidths[199]=8;
  117. fontwidths[200]=8;
  118. fontwidths[201]=8;
  119. fontwidths[202]=8;
  120. fontwidths[203]=8;
  121. fontwidths[204]=3;
  122. fontwidths[205]=3;
  123. fontwidths[206]=3;
  124. fontwidths[207]=3;
  125. fontwidths[208]=8;
  126. fontwidths[209]=8;
  127. fontwidths[210]=9;
  128. fontwidths[211]=9;
  129. fontwidths[212]=9;
  130. fontwidths[213]=9;
  131. fontwidths[214]=9;
  132. fontwidths[216]=9;
  133. fontwidths[217]=8;
  134. fontwidths[218]=8;
  135. fontwidths[219]=8;
  136. fontwidths[220]=8;
  137. fontwidths[221]=8;
  138. fontwidths[222]=8;
  139. fontwidths[223]=7;
  140. fontwidths[224]=6;
  141. fontwidths[225]=6;
  142. fontwidths[226]=6;
  143. fontwidths[227]=6;
  144. fontwidths[228]=6;
  145. fontwidths[229]=6;
  146. fontwidths[230]=11;
  147. fontwidths[231]=6;
  148. fontwidths[232]=6;
  149. fontwidths[233]=6;
  150. fontwidths[234]=6;
  151. fontwidths[235]=6;
  152. fontwidths[236]=3;
  153. fontwidths[237]=3;
  154. fontwidths[238]=3;
  155. fontwidths[239]=3;
  156. fontwidths[240]=6;
  157. fontwidths[241]=6;
  158. fontwidths[242]=6;
  159. fontwidths[243]=6;
  160. fontwidths[244]=6;
  161. fontwidths[245]=6;
  162. fontwidths[246]=6;
  163. fontwidths[248]=7;
  164. fontwidths[249]=6;
  165. fontwidths[250]=6;
  166. fontwidths[251]=6;
  167. fontwidths[252]=6;
  168. fontwidths[253]=6;
  169. fontwidths[254]=6;
  170. fontwidths[255]=6;
  171. function printf(stringToPrint)
  172. {
  173. }
  174. function clear()
  175. {
  176. }
  177. function onLoadScript() 
  178. {
  179. answerDiv = document.getElementById('answerDiv');
  180. formulaDiv = document.getElementById('formulaDiv');
  181. variableCacheDiv = document.getElementById('variableCacheDiv');
  182. formulaCacheDiv = document.getElementById('formulaCacheDiv');    
  183. formulaCacheParentDiv = document.getElementById('formulaCacheParentDiv');    
  184. consolediv = document.getElementById('consoleDiv');
  185. cursordiv = document.getElementById('cursorDiv');
  186. loadAppleButtons();
  187. scrollbar = new AppleVerticalScrollbar(document.getElementById("formulaCacheScrollBar"));
  188. scrollArea = new AppleScrollArea(document.getElementById("formulaCacheParentDiv"), scrollbar);
  189. scrollArea.scrollsHorizontally = false;
  190. scrollArea.singlepressScrollPixels = 25;
  191. answerscrollbar = new AppleVerticalScrollbar(document.getElementById("answerCacheScrollBar"));
  192. answerscrollArea = new AppleScrollArea(document.getElementById("answerCacheParentDiv"), answerscrollbar);
  193. answerscrollArea.scrollsHorizontally = false;
  194. answerscrollArea.singlepressScrollPixels = 25;    
  195. scrollbar._track.style.height='10px';
  196. scrollbar._track.children[0].style.background = "url(" + scrollbar.trackMiddlePath + ") repeat-y top left";
  197. scrollbar._track.children[2].style.background = "url(" + scrollbar.trackMiddlePath + ") repeat-y top left";
  198. answerscrollbar._track.children[0].style.background = "url(" + scrollbar.trackMiddlePath + ") repeat-y top left";
  199. answerscrollbar._track.children[2].style.background = "url(" + scrollbar.trackMiddlePath + ") repeat-y top left";
  200. formulascrollbar = new AppleHorizontalScrollbar(document.getElementById("formulaScrollBar"));
  201. formulascrollArea = new AppleScrollArea(document.getElementById("formulaParentDiv"), formulascrollbar);
  202. formulascrollArea.scrollsVertically = false;
  203. formulascrollArea.singlepressScrollPixels = 25;
  204. tdstretch = new PDWindowStretch();
  205. loadPreferences();
  206. implementPreferences();
  207. setLocalization();
  208. updateDisplay();
  209. checkForUpdates();
  210. if (window.widget) {
  211. widget.setPreferenceForKey(thisVersionNumber,"thisVersionNumber");
  212. }
  213. if (commaLocale) {
  214. document.getElementById('radixPoint').src="images/numbers/comma.png";
  215. }
  216. }
  217. function loadPreferences()
  218. {
  219. if (window.widget) {
  220. if (widget.preferenceForKey("preferencesPrecision")) { 
  221. preferencesPrecision=parseInt(widget.preferenceForKey("preferencesPrecision"));
  222. if (preferencesPrecision<0 || preferencesProgrammerMode>16) {
  223. preferencesPrecision=0;
  224. }
  225. }
  226. if (widget.preferenceForKey("preferencesProgrammerMode")) { 
  227. preferencesProgrammerMode=parseInt(widget.preferenceForKey("preferencesProgrammerMode"));
  228. if (preferencesProgrammerMode!==0 && preferencesProgrammerMode!==1) {
  229. preferencesProgrammerMode=0;
  230. }
  231. }
  232. if (widget.preferenceForKey("preferencesAnswerFormatting")) { 
  233. preferencesAnswerFormatting=parseInt(widget.preferenceForKey("preferencesAnswerFormatting"));
  234. if (preferencesProgrammerMode) {
  235. if (preferencesAnswerFormatting<1 || preferencesAnswerFormatting>7) {
  236. preferencesAnswerFormatting=1;
  237. }
  238. } else {
  239. if (preferencesAnswerFormatting<1 || preferencesAnswerFormatting>4) {
  240. preferencesAnswerFormatting=1;
  241. }
  242. }
  243. }
  244. if (!isNaN(parseInt(widget.preferenceForKey("preferencesRadians")))) { 
  245. preferencesRadians=parseInt(widget.preferenceForKey("preferencesRadians"));
  246. if (preferencesRadians!==0 && preferencesRadians!==1) {
  247. preferencesRadians=1;
  248. }
  249. }
  250. if (!isNaN(parseInt(widget.preferenceForKey("preferencesThousandsSeperators")))) { 
  251. preferencesThousandsSeperators=parseInt(widget.preferenceForKey("preferencesThousandsSeperators"));
  252. if (preferencesThousandsSeperators!==0 && preferencesThousandsSeperators!==1) {
  253. preferencesThousandsSeperators=0;
  254. }
  255. }
  256. if (widget.preferenceForKey("preferencesIgnoreUpdates")) { 
  257. preferencesIgnoreUpdates=parseInt(widget.preferenceForKey("preferencesIgnoreUpdates"));
  258. if (preferencesIgnoreUpdates!==0 && preferencesIgnoreUpdates!==1) {
  259. preferencesIgnoreUpdates=0;
  260. }
  261. }
  262. }
  263. }
  264. function implementPreferences()
  265. {
  266. if (preferencesProgrammerMode) {
  267. fseLength=7;
  268. document.getElementById('widgetBackProgrammerMode').checked=true;
  269. }
  270. formatPreferenceBar();
  271. if (preferencesPrecision) {
  272. document.getElementById('widgetBackPrecision').value=preferencesPrecision;
  273. }
  274. if (preferencesThousandsSeperators) {
  275. document.getElementById('widgetBackThousandsSeparators').checked=true;
  276. } else {
  277. document.getElementById('widgetBackThousandsSeparators').checked=false;
  278. }
  279. changePreferencesAnswerFormatting(preferencesAnswerFormatting);
  280. changePreferencesRadians(preferencesRadians);
  281. }
  282. function loadAppleButtons() 
  283. {
  284. flipper = new AppleInfoButton(document.getElementById("flipper"), document.getElementById("front"), "white", "white", showBack);
  285. expander = new AppleInfoButton(document.getElementById("expander"), document.getElementById("front"), "white", "white", expandCalculator);
  286. glassButton = new AppleGlassButton(document.getElementById("doneButton"), "Done", showFront);
  287. expander._flipLabel.src="images/infoBar/plus.png";
  288. }
  289. function pasteHandler(event)
  290. {
  291. var tempVar=event.clipboardData.getData('Text');
  292. var i, stringToAdd='', curkeycode;
  293. if (commaLocale) {
  294. tempVar=tempVar.replace(/\./g,',');
  295. } else {
  296. tempVar=tempVar.replace(/,/g,'.');
  297. }
  298. for (i=0; i<tempVar.length; i++) {
  299. curkeycode=tempVar.charCodeAt(i);
  300. if ((curkeycode>47 && curkeycode<58) || (curkeycode>64 && curkeycode<91) || (curkeycode>96 && curkeycode<123) || (curkeycode>191 && curkeycode<256 && curkeycode!=215 && curkeycode!=237)) {
  301. stringToAdd=stringToAdd+''+tempVar.charAt(i);
  302. } else {
  303. switch (curkeycode) {
  304. case 37: case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47: case 61: case 94:
  305. stringToAdd=stringToAdd+''+tempVar.charAt(i);
  306. break;
  307. }
  308. }
  309. if (tempVar.charCodeAt(i)==10 || tempVar.charCodeAt(i)==3 || tempVar.charCodeAt(i)==13) 
  310. break;
  311. }
  312. addToMainDisplay(stringToAdd);
  313. updateDisplay();
  314. event.preventDefault();
  315. event.stopPropagation();
  316. }
  317. function copyHandler(event)
  318. {
  319. if (preferencesWhatToCopy) {
  320. event.clipboardData.setData('Text', formula);
  321. } else
  322. event.clipboardData.setData('Text', answerDiv.innerHTML);
  323. event.preventDefault();
  324. event.stopPropagation();
  325. }
  326. function showCursorLocation(event)
  327. {
  328. var xClickLocation=event.pageX-cursorPixelOffset+formulascrollArea.content.scrollLeft; 
  329. if (getFormulaPixelLength()<=xClickLocation) { 
  330. currentcursorposition=0;
  331. updateCursorDiv();
  332. } else {
  333. getCursorPositionWithCoords(xClickLocation);
  334. }
  335. preferencesWhatToCopy=1;
  336. }
  337. function getFormulaPixelLength()
  338. {
  339. var i, pixelLength=0, formulaLength=formula.length;
  340. for (i=0; i<formulaLength; i++) {
  341. pixelLength+=fontwidths[formula.charCodeAt(i)]+1;
  342. }
  343. return pixelLength;
  344. }
  345. function getCursorPositionWithCoords(xlocation)
  346. {    
  347. justCalculatedEquation=false;
  348. var i, cursorpositionpixels=0;
  349. for (i=0; i<=formula.length; i++) {
  350. if (cursorpositionpixels>=xlocation) {
  351. currentcursorposition=i-formula.length-1;
  352. currentcursorposition++;
  353. if ((cursorpositionpixels-xlocation)>(fontwidths[formula.charCodeAt(i-1)]-2)*0.66) 
  354. {
  355. currentcursorposition--;
  356. cursorpositionpixels-=(fontwidths[formula.charCodeAt(i-1)]+1);
  357. }
  358. break;
  359. }
  360. cursorpositionpixels+=fontwidths[formula.charCodeAt(i)]+1;
  361. }
  362. cursordiv.style.marginLeft=Math.round(cursorpositionpixels);
  363. }
  364. function updateCursorDiv()
  365. {    
  366. var i, cursorpositionpixels=0;
  367. var spottogoto=formula.length-1+currentcursorposition;
  368. for (i=0; i<=spottogoto; i++)
  369. {
  370. cursorpositionpixels+=fontwidths[formula.charCodeAt(i)]+1;
  371. }
  372. if (!isNaN(cursorpositionpixels))
  373. {
  374. cursordiv.style.marginLeft=Math.round(cursorpositionpixels);
  375. }
  376. else
  377. {    
  378. if (isNaN(cursorpositionpixels)) cursordiv.style.marginLeft=0;
  379. else cursordiv.style.marginLeft=cursorPixelOffset;
  380. }
  381. updateFormulaScrollArea();
  382. }
  383. function addFormulaToCache(formula) {
  384. formulaCacheDiv.innerHTML=formulaCacheDiv.innerHTML + '<div id=\"formulaCacheDiv' + curformula + '\" onclick=\"recalculateCachedFormula(this);\">' + formula + '</div>';
  385. curformula++;
  386. scrollArea.refresh();    
  387. var tempvar=scrollArea.content.scrollHeight - scrollArea.viewHeight-14;
  388. if (tempvar<=0 || tempvar==scrollArea.content.scrollTop) scrollArea.verticalScrollTo(scrollArea.content.scrollHeight - scrollArea.viewHeight);
  389. }
  390. function addVariableToMainDisplay(variableName)
  391. {
  392. if (justCalculatedEquation)
  393. formula='';
  394. addToMainDisplay(variableName);
  395. updateDisplay();
  396. justCalculatedEquation=false;
  397. }
  398. function updateVariableCacheDivScrollArea()
  399. {
  400. answerscrollArea.refresh();
  401. var tempvar=answerscrollArea.content.scrollHeight - answerscrollArea.viewHeight-14;
  402. if (tempvar<=0 || tempvar==answerscrollArea.content.scrollTop) 
  403. answerscrollArea.verticalScrollTo(answerscrollArea.content.scrollHeight - answerscrollArea.viewHeight);
  404. }
  405. function addToMainDisplay(numbertoadd)
  406. {
  407. var formulaLength=formula.length;
  408. document.getElementById("tempDiv").innerHTML=numbertoadd;
  409. numbertoadd=document.getElementById("tempDiv").innerHTML;
  410. formula=formula.substring(0, formulaLength+currentcursorposition)+''+numbertoadd+formula.substring(formulaLength+currentcursorposition, formulaLength);
  411. updateDisplay();
  412. }
  413. function keyPressed(event)
  414. {
  415. if (event.metaKey) return; 
  416. if (event.shiftKey) {
  417. switch (event.charCode) {
  418. case 8: 
  419. keyPressHandler(63289);
  420. break;
  421. case 63234: 
  422. keyPressHandler(-63234);
  423. break;
  424. case 63235: 
  425. keyPressHandler(-63235);
  426. break;
  427. default:
  428. keyPressHandler(event.charCode);
  429. break;
  430. }
  431. } else 
  432. keyPressHandler(event.charCode);
  433. event.preventDefault();
  434. }
  435. function keyPressHandler(keypressedcode)
  436. {
  437. var curlength;
  438. var curenternum=0;
  439. if (keypressedcode==3 || keypressedcode==13) {
  440. displayAnswer(formula);
  441. } else if (keypressedcode==63289) { 
  442. if (previousKeyEntered==63289) 
  443. resetWidget();
  444. else
  445. clearDisplay();
  446. } else if (keypressedcode==63272) { 
  447. if (currentcursorposition<0) {
  448. curlength=formula.length;
  449. formula=formula.substring(0, curlength+currentcursorposition)+''+ formula.substring(curlength+1+currentcursorposition, curlength);
  450. currentcursorposition++; 
  451. }
  452. updateDisplay();
  453. } else if (keypressedcode==-63234) { 
  454. if (--preferencesAnswerFormatting<1)
  455. preferencesAnswerFormatting=fseLength;
  456. changePreferencesAnswerFormatting(preferencesAnswerFormatting);
  457. } else if (keypressedcode==-63235) { 
  458. if (++preferencesAnswerFormatting>fseLength)
  459. preferencesAnswerFormatting=1;
  460. changePreferencesAnswerFormatting(preferencesAnswerFormatting);
  461. } else if (keypressedcode==63234) { 
  462. currentcursorposition--;
  463. if (formula.length-1+currentcursorposition<-1)
  464. currentcursorposition++;
  465. updateCursorDiv();
  466. } else if (keypressedcode==63235) { 
  467. currentcursorposition++;
  468. if (currentcursorposition>0) currentcursorposition=0;
  469. updateCursorDiv();
  470. } else if (keypressedcode==63232) {
  471. currentcursorposition=0-formula.length;
  472. updateCursorDiv();
  473. } else if (keypressedcode==63233) { 
  474. currentcursorposition=0;
  475. updateCursorDiv();
  476. } else if (keypressedcode==8) { 
  477. curlength=formula.length;
  478. formula=formula.substring(0, curlength-1+currentcursorposition)+'' + formula.substring(curlength+currentcursorposition, curlength);
  479. updateDisplay();
  480. } else if ((keypressedcode>47 && keypressedcode<58) || (keypressedcode>64 && keypressedcode<91) || (keypressedcode>96 && keypressedcode<123) || (keypressedcode>191 && keypressedcode<256 && keypressedcode!=215 && keypressedcode!=237)) { 
  481. curenternum=String.fromCharCode(keypressedcode);
  482. if (justCalculatedEquation && curenternum && !justRecalculatedCachedFormula) {
  483. formula='';
  484. currentcursorposition=0;
  485. }
  486. addToMainDisplay(curenternum);
  487. } else { 
  488. switch(keypressedcode)
  489. {
  490. case 37: curenternum='%'; break;
  491. case 40: curenternum='('; break;
  492. case 41: curenternum=')'; break;
  493. case 42: curenternum='*'; 
  494. if (justCalculatedEquation && !justRecalculatedCachedFormula) {
  495. curenternum='ans*';
  496. currentcursorposition=0;
  497. }
  498. break;
  499. case 43: curenternum='+';
  500. if (justCalculatedEquation && !justRecalculatedCachedFormula) {
  501. curenternum='ans+';
  502. currentcursorposition=0;
  503. }
  504. break;
  505. case 44: 
  506. if (commaLocale)
  507. curenternum=','; 
  508. else 
  509. curenternum='.'; break;
  510. case 45: curenternum='-';
  511. if (justCalculatedEquation && !justRecalculatedCachedFormula) {
  512. curenternum='ans-';
  513. currentcursorposition=0;
  514. }
  515. break;
  516. case 46: 
  517. if (commaLocale)
  518. curenternum=','; 
  519. else 
  520. curenternum='.'; break;
  521. case 47: curenternum='/'; 
  522. if (justCalculatedEquation && !justRecalculatedCachedFormula) {
  523. curenternum='ans/';
  524. currentcursorposition=0;
  525. }
  526. break;
  527. case 61: curenternum='='; break;
  528. case 94: curenternum='^'; 
  529. if (justCalculatedEquation && !justRecalculatedCachedFormula) {
  530. curenternum='ans^';
  531. currentcursorposition=0;
  532. }
  533. break;
  534. }
  535. if (justCalculatedEquation && curenternum && !justRecalculatedCachedFormula) {
  536. formula='';
  537. currentcursorposition=0;
  538. }
  539. addToMainDisplay(curenternum);
  540. }
  541. if (keypressedcode!=3 && keypressedcode!=13 && keypressedcode!=61)
  542. justCalculatedEquation=false;
  543. justRecalculatedCachedFormula=false;
  544. previousKeyEntered=keypressedcode;
  545. setTimeout("previousKeyEntered='';", 400);
  546. }
  547. var calculationTimerCounter=0;
  548. var calculationTimer;
  549. function displayAnswer(equationToCalculate)
  550. {
  551. answer=calculateEquation(equationToCalculate);
  552. answerDiv.style.fontSize='17px';
  553. answerDiv.style.top='35px';
  554. if (answer.errorMessage) { 
  555. answerDiv.style.fontSize='12px';
  556. answerDiv.style.top='38px';
  557. answerDiv.innerHTML='Error: '+answer.errorMessage;
  558. } else { 
  559. finalAnswer=new PDAnswer(answer.answer);
  560. answerDiv.innerHTML=finalAnswer.getAnswer(preferencesAnswerFormatting);
  561. setVariableInCacheDisplay(answer.answer, 'ans');
  562. if (answer.variableStoredIn) {
  563. answerDiv.innerHTML=answer.variableStoredIn+"="+answerDiv.innerHTML;
  564. updateAnswerCache(answer.answer,0);
  565. setVariableInCacheDisplay(answer.answer, answer.variableStoredIn);
  566. } else {
  567. updateAnswerCache(answer.answer);
  568. setVariableInCacheDisplay(answer.answer, 'ans'+(currentAnswerNumber-1));
  569. }
  570. if (!justRecalculatedCachedFormula)
  571. addFormulaToCache(equationToCalculate);
  572. justCalculatedEquation=true;
  573. }
  574. calculationCount++;
  575. if (calculationCount>30) {
  576. checkForUpdates();
  577. calculationCount=1;
  578. }
  579. }
  580. function redisplayAnswer()
  581. {
  582. if (!answer.errorMessage) {
  583. answerDiv.innerHTML=finalAnswer.getAnswer(preferencesAnswerFormatting);
  584. }
  585. }
  586. function clearDisplay()
  587. {
  588. resetVariables();
  589. updateDisplay();
  590. }
  591. function resetWidget()
  592. {
  593. resetVariables();
  594. answerDiv.style.fontSize='17px';
  595. variableNames=new Array();
  596. variables=new Array();
  597. variableIndex=0;
  598. setVariable('pi', Math.PI);
  599. answer={errorMessage:'1', answer:''};
  600. currentAnswerNumber=1;
  601. curformula=1;
  602. answerDiv.innerHTML='0';
  603. formulaCacheDiv.innerHTML='';
  604. variableCacheDiv.innerHTML="";
  605. answerscrollArea.refresh();
  606. answerscrollbar.refresh();
  607. scrollArea.refresh();
  608. scrollbar.refresh();
  609. updateDisplay();
  610. calculationCount=0;
  611. checkForUpdates();
  612. }
  613. function resetVariables()
  614. {
  615. formula='';
  616. currentcursorposition=0;
  617. }
  618. function updateDisplay()
  619. {
  620. updateCursorDiv();
  621. formulaDiv.innerHTML=formula; 
  622. clear();
  623. updateFormulaScrollArea();
  624. }
  625. function updateFormulaScrollArea()
  626. {
  627. formulaDiv.style.width=getFormulaPixelLength()+1; 
  628. formulascrollArea.refresh();
  629. formulascrollbar.refresh();
  630. formulaBarWidth=parseInt(document.getElementById("formulaParentDiv").style.width);
  631. if (formulascrollArea.content.scrollWidth>formulaBarWidth) {
  632. if (parseInt(cursordiv.style.marginLeft)<formulascrollArea.content.scrollLeft) { 
  633. formulascrollArea.horizontalScrollTo(parseInt(cursordiv.style.marginLeft));
  634. } else if (parseInt(cursordiv.style.marginLeft)>(formulascrollArea.content.scrollLeft+formulaBarWidth)) {
  635. formulascrollArea.horizontalScrollTo(parseInt(cursordiv.style.marginLeft)-formulaBarWidth+1);
  636. }
  637. }
  638. }
  639. function recalculateCachedFormula(curformuladiv)
  640. {
  641. resetVariables();
  642. formula=curformuladiv.innerHTML;
  643. updateDisplay();
  644. justRecalculatedCachedFormula=true;
  645. displayAnswer(formula);
  646. }
  647. function expandCalculator()
  648. {
  649. if (tdstretch.toWidth) { 
  650. expander._flipLabel.src="images/infoBar/minus.png";
  651. document.getElementById("resize").style.display = 'block';
  652. if (window.widget) window.resizeTo(306+tdstretch.maxWidth, 243+tdstretch.maxHeight);
  653. } else {
  654. expander._flipLabel.src="images/infoBar/plus.png";
  655. }
  656. tdstretch.stretch(event);
  657. }
  658. function setWidgetWidth(offset)
  659. {
  660. var off297=offset+297;
  661. var off279=offset+279;
  662. document.getElementById("topShadow").style.width=off279;
  663. document.getElementById("topShadowRight").style.left=off279;
  664. document.getElementById("formulaBar").style.width=off279;
  665. document.getElementById("formulaBarRight").style.left=off279;
  666. document.getElementById("preferencesBar").style.width=off297;
  667. document.getElementById("preferencesBarRight").style.left=off297;
  668. document.getElementById("mainStretchingBody").style.width = off297;
  669. document.getElementById("mainStretchingBodyRight").style.left=off297;
  670. document.getElementById("infoBar").style.width = off279;
  671. document.getElementById("infoBarRight").style.left=off279;
  672. document.getElementById("bottomShadow").style.width = off279;
  673. document.getElementById("bottomShadowRight").style.left=off279;
  674. document.getElementById("formulaParentDiv").style.width=250+offset;
  675. document.getElementById("preferencesBarContainer").style.width=288+offset;
  676. document.getElementById("preferenceBarShadow").style.width=288+offset;
  677. document.getElementById("answerDiv").style.width=247+offset;
  678. document.getElementById("answerCacheParentDiv").style.width=116+offset;
  679. document.getElementById("resize").style.left = 278+offset;
  680. if (offset<120) { 
  681. document.getElementById("bottomHighlightDiv").style.width=283+offset-offset/120*18;
  682. } else {
  683. document.getElementById("bottomHighlightDiv").style.width=265+offset;
  684. document.getElementById("variableCacheDiv").style.width=offset-4;
  685. document.getElementById("answerCacheContainerDiv").style.width=offset-2;
  686. document.getElementById("formulaCacheContainerDiv").style.width=288+offset;
  687. document.getElementById("formulaCacheParentDiv").style.width=281+offset;
  688. }
  689. }
  690. function setWidgetHeight(offset)
  691. {
  692. var off130=130+offset;
  693. document.getElementById("mainStretchingBody").style.height = off130;
  694. document.getElementById("mainStretchingBodyRight").style.height = off130;
  695. document.getElementById("mainBodyBreak").style.height = off130;
  696. document.getElementById("bottomHighlightDiv").style.top =212+offset;
  697. document.getElementById("resize").style.top =213+offset;
  698. document.getElementById("infoBar").style.top =208+offset;
  699. document.getElementById("infoBarRight").style.top =208+offset;
  700. document.getElementById("bottomShadow").style.top =230+offset;
  701. document.getElementById("bottomShadowRight").style.top =230+offset;
  702. if (offset>99) { 
  703. document.getElementById("formulaCacheScrollBar").style.height=offset-2;
  704. document.getElementById("formulaCacheParentDiv").style.height=offset-2;
  705. scrollArea.verticalScrollTo(scrollArea.content.scrollTop);
  706. scrollArea.refresh();
  707. }
  708. }
  709. function resizeMouseDown(event)
  710. {
  711. document.addEventListener("mousemove", resizeMouseMove, true);
  712. document.addEventListener("mouseup", resizeMouseUp, true);
  713. var tempx=parseInt(document.getElementById("resize").style.left);
  714. var tempy=parseInt(document.getElementById("resize").style.top);
  715. var initialWidth=parseInt(document.getElementById("preferencesBar").style.width);
  716. var initialHeight=parseInt(document.getElementById("mainBodyBreak").style.height);
  717. initialClick={x:event.x, y:event.y};
  718. initialValues={initialWidthOffset:(initialWidth-297), initialHeightOffset:(initialHeight-130)};
  719. event.stopPropagation();
  720. event.preventDefault();
  721. }
  722. function resizeMouseMove(event)
  723. {
  724. var x = event.x-initialClick.x; 
  725. var y = event.y-initialClick.y;
  726. var offsetx=initialValues.initialWidthOffset+x, offsety=initialValues.initialHeightOffset+y;
  727. if (offsetx>123) { 
  728. setWidgetWidth(offsetx);
  729. } else { 
  730. setWidgetWidth(120);
  731. offsetx=120;
  732. }
  733. if (offsety>103) 
  734. {
  735. setWidgetHeight(offsety);
  736. } else {
  737. setWidgetHeight(100);
  738. offsety=100;
  739. }
  740. if (window.widget) window.resizeTo(offsetx+306,offsety+243);
  741. event.stopPropagation();
  742. event.preventDefault();
  743. }
  744. function resizeMouseUp(event)
  745. {
  746. document.removeEventListener("mousemove", resizeMouseMove, true);
  747. document.removeEventListener("mouseup", resizeMouseUp, true); 
  748. var x = event.x-initialClick.x; 
  749. var y = event.y-initialClick.y;
  750. var offsetx=initialValues.initialWidthOffset+x, offsety=initialValues.initialHeightOffset+y;
  751. if (offsetx<120)
  752. offsetx=120;
  753. if (offsety<100)
  754. offsety=100;
  755. tdstretch.maxWidth=offsetx;
  756. tdstretch.maxHeight=offsety;
  757. tdstretch.fromWidth=offsetx;
  758. tdstretch.fromHeight=offsety;
  759. event.stopPropagation();
  760. event.preventDefault();
  761. }
  762. function changePreferencesAnswerFormatting(toSwitchTo) 
  763. {
  764. var imagepath='images/preferencesBar/';
  765. var onArray=new Array('','','','','','','');
  766. onArray[toSwitchTo-1]='Selected';
  767. preferencesAnswerFormatting=toSwitchTo;
  768. document.getElementById('preferenceBarFlo').src=imagepath+'flo'+onArray[0]+'.png';
  769. document.getElementById('preferenceBarSci').src=imagepath+'sci'+onArray[1]+'.png';
  770. document.getElementById('preferenceBarEng').src=imagepath+'eng'+onArray[2]+'.png';
  771. document.getElementById('preferenceBarPercent').src=imagepath+'percent'+onArray[3]+'.png';
  772. document.getElementById('preferenceBarBin').src=imagepath+'bin'+onArray[4]+'.png';
  773. document.getElementById('preferenceBarOct').src=imagepath+'oct'+onArray[5]+'.png';
  774. document.getElementById('preferenceBarHex').src=imagepath+'hex'+onArray[6]+'.png';
  775. redisplayAnswer();
  776. if (window.widget) {
  777. widget.setPreferenceForKey(preferencesAnswerFormatting,"preferencesAnswerFormatting");
  778. }
  779. }
  780. function changePreferencesRadians(toSwitchTo)
  781. {
  782. var imagepath='images/preferencesBar/';
  783. var onArray=new Array('','');
  784. onArray[toSwitchTo]='Selected';
  785. document.getElementById('preferenceBarRad').src=imagepath+'rad'+onArray[1]+'.png';
  786. document.getElementById('preferenceBarDeg').src=imagepath+'deg'+onArray[0]+'.png';
  787. preferencesRadians=toSwitchTo;
  788. if (window.widget) {
  789. widget.setPreferenceForKey(preferencesRadians,"preferencesRadians");
  790. }
  791. }
  792. function changePreferencesProgrammerMode()
  793. {
  794. if (document.getElementById("widgetBackProgrammerMode").checked) {
  795. preferencesProgrammerMode=1;
  796. formatPreferenceBar();
  797. } else {
  798. preferencesProgrammerMode=0;
  799. formatPreferenceBar();
  800. }
  801. if (preferencesProgrammerMode) {
  802. fseLength=7;
  803. } else {
  804. fseLength=4;
  805. if (preferencesAnswerFormatting>4) {
  806. preferencesAnswerFormatting=1;
  807. }
  808. }
  809. changePreferencesAnswerFormatting(preferencesAnswerFormatting);
  810. if (window.widget) {
  811. widget.setPreferenceForKey(preferencesProgrammerMode,"preferencesProgrammerMode");
  812. }
  813. }
  814. function formatPreferenceBar()
  815. {
  816. if (preferencesProgrammerMode) { 
  817. document.getElementById('preferenceBarFlo').style.left='1%';
  818. document.getElementById('preferenceBarSci').style.left='11%';
  819. document.getElementById('preferenceBarEng').style.left='21%';
  820. document.getElementById('preferenceBarPercent').style.left='30%';
  821. document.getElementById('preferenceBarBin').style.display='block';
  822. document.getElementById('preferenceBarOct').style.display='block';
  823. document.getElementById('preferenceBarHex').style.display='block';
  824. document.getElementById('preferenceBarDivider').style.left='73%';
  825. document.getElementById('preferenceBarRad').style.left='77%';
  826. document.getElementById('preferenceBarDeg').style.left='87%';
  827. } else { 
  828. document.getElementById('preferenceBarFlo').style.left='2%';
  829. document.getElementById('preferenceBarSci').style.left='16%';
  830. document.getElementById('preferenceBarEng').style.left='30%';
  831. document.getElementById('preferenceBarPercent').style.left='44%';
  832. document.getElementById('preferenceBarBin').style.display='none';
  833. document.getElementById('preferenceBarOct').style.display='none';
  834. document.getElementById('preferenceBarHex').style.display='none';
  835. document.getElementById('preferenceBarDivider').style.left='61%';
  836. document.getElementById('preferenceBarRad').style.left='69%';
  837. document.getElementById('preferenceBarDeg').style.left='84%';
  838. }
  839. }
  840. function changePreferencesPrecision()
  841. {
  842. preferencesPrecision=parseInt(document.getElementById("widgetBackPrecision").value);
  843. if (calculationCount) {
  844. redisplayAnswer();
  845. }
  846. if (window.widget) {
  847. widget.setPreferenceForKey(preferencesPrecision,"preferencesPrecision");
  848. }
  849. }
  850. function changePreferencesThousandsSeparators()
  851. {
  852. if (document.getElementById("widgetBackThousandsSeparators").checked)
  853. preferencesThousandsSeperators=1;
  854. else
  855. preferencesThousandsSeperators=0;
  856. if (calculationCount) {
  857. finalAnswer.reformatNumbers();
  858. redisplayAnswer();
  859. }
  860. if (window.widget) {
  861. widget.setPreferenceForKey(preferencesThousandsSeperators,"preferencesThousandsSeperators");
  862. }
  863. }
  864. function turnOnNumber(currentImage, imageName)
  865. {
  866. currentImage.src='images/numbers/'+imageName+'on.png';
  867. }
  868. function turnOffNumber(currentImage, imageName)
  869. {
  870. currentImage.src='images/numbers/'+imageName+'.png';
  871. }
  872. function PDWindowStretch() 
  873. {
  874. this.duration = 300;
  875. this.multiplier = 1;    
  876. this.minWidth=0;
  877. this.maxWidth=120;
  878. this.minHeight=0;
  879. this.maxHeight=100;
  880. this.fromWidth = this.minWidth;
  881. this.toWidth = this.maxWidth;
  882. this.fromHeight = this.minHeight;
  883. this.toHeight = this.maxHeight;
  884. this.stretchAnimator = null;
  885. var self=this;
  886. this.nextFrame = function (animation, now, first, done) 
  887. {
  888. setWidgetWidth(now);
  889. }
  890. this.nextFrameHeight = function (animation, now, first, done) 
  891. {
  892. setWidgetHeight(now);
  893. document.getElementById('resize').style.opacity=now/100;
  894. }
  895. this.flipWidth = function () 
  896. {
  897. if (self.toWidth == self.maxWidth) { 
  898. self.toWidth = self.minWidth;
  899. self.fromWidth = self.maxWidth;
  900. } else {  
  901. self.toWidth = self.maxWidth;
  902. self.fromWidth = self.minWidth;
  903. document.getElementById("resize").style.display = 'none';
  904. }
  905. }
  906. this.flipHeight = function () 
  907. {
  908. if (self.toHeight == self.maxHeight) { 
  909. self.toHeight = self.minHeight;
  910. self.fromHeight = self.maxHeight;
  911. } else { 
  912. document.getElementById("mainStretchingBody").style.height = '130px';
  913. document.getElementById("mainStretchingBodyRight").style.height = '130px';
  914. document.getElementById("mainBodyBreak").style.height = '130px';
  915. document.getElementById("bottomHighlightDiv").style.top = '212px';
  916. if (window.widget) window.resizeTo(306, 243);
  917. self.toHeight = self.maxHeight;
  918. self.fromHeight = self.minHeight;
  919. }
  920. }
  921. this.stretch = function (event)
  922. {
  923. this.multiplier = 1;
  924. if (event.shiftKey)
  925. this.multiplier = 10; 
  926. if (this.stretchAnimator)  {
  927. this.stretchAnimator = new AppleAnimator(this.duration * this.multiplier, 13, this.fromWidth, this.toWidth, this.nextFrame);
  928. this.stretchAnimatorHeight = new AppleAnimator(this.duration * this.multiplier, 13, this.fromHeight, this.toHeight, this.nextFrameHeight);
  929. this.stretchAnimator.oncomplete = this.flipWidth;
  930. this.stretchAnimatorHeight.oncomplete = this.flipHeight;
  931. this.stretchAnimator.start();
  932. this.stretchAnimatorHeight.start();
  933. }
  934. }
  935. function setPreferencesWhatToCopy(whatToSet)
  936. {
  937. preferencesWhatToCopy=whatToSet;
  938. }
  939. function showBack(event) {
  940. if (window.widget) {
  941. widget.prepareForTransition("ToBack");
  942. }
  943. document.getElementById("front").style.display = "none";
  944. document.getElementById("back").style.display = "block";
  945. if (window.widget) {
  946. setTimeout("widget.performTransition()", 0);
  947. }
  948. }    
  949. function showFront(event) {
  950. if (window.widget) {
  951. widget.prepareForTransition("ToFront");
  952. }
  953. document.getElementById("back").style.display = "none";
  954. document.getElementById("front").style.display = "block";
  955. if (window.widget) {
  956. setTimeout("widget.performTransition()", 0);
  957. }
  958. }
  959. function setLocalization()
  960. {    
  961. if (window.widget) {
  962. try {
  963. var calc=widget.calculator;
  964. gDecimalSeparator = calc.evaluateExpression ("decimal_string", 16);
  965. if (gDecimalSeparator===undefined) {
  966. } else if (gDecimalSeparator==',') {
  967. commaLocale=1;
  968. }
  969. } catch (ex) {}
  970. }
  971. }
  972. //Thanks was good, FlightMech is lame.
  973. //It's pretty late, and aim is to blame.
  974. //I talked to a guy, he worked at this place,
  975. //Where robots are built that fly around space.
  976. //They thought I was dumb, or so they told me.
  977. //But they just lost a satellite. Now that... is... irony.
  978.